//test the uniform random number generator
#include <stdio.h>
#include "uniform.c"
#include "expon.c"

#define SIZE 5000 //generate 5000 random variate

#define NUM 20 // the number of interval is 20

#define A 0 // the value range is from 0 to 2

#define B 2

double uniform(double *);
double expon(double *, float);

int main() {

  int i,j;
  double mean,sum;
  double seed;
  double r;
  double h[NUM];
  double a,b,num,interval;

  //set initial values
  sum=0;
  a=A;b=B;num=NUM;
  interval=(b-a)/num;
  seed = 1234567; //set the seed only once, at the start.
  for (j=0;j<NUM;j++){h[j]=0;}
  //end initial values

  for (i=0; i < SIZE; i++) {
	  r = uniform(&seed); //generate an uniform variate
	  for (j=0;j<NUM;j++){
		  if ((a+interval*j)<=r&&r<(a+interval*(j+1))){ //judge which interval it belongs to
			  h[j]++;}
	  }		  
	  sum=sum+r; 
  }
  mean=sum/SIZE; //compute the mean

  for (j=0;j<NUM;j++){
	  printf("\n #%d \t%g~%g\t",j+1,(a+interval*j),(a+interval*(j+1)));
	  for (i=0; i < h[j]/10; i++)
		  printf("*"); //print the histogram graph
  }
  printf("\n The mean is %g\n\n",mean); //print the mean

}
